package com.huawei.hms.framework.common;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build.VERSION;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.LinkedList;

public class NetworkUtil {
    private static final String TAG = NetworkUtil.class.getSimpleName();
    private static final int TYPE_WIFI_P2P = 13;

    public static String getDnsServerIps(Context context) {
        return Arrays.toString(getDnsServerIpsFromConnectionManager(context));
    }

    private static String[] getDnsServerIpsFromConnectionManager(Context context) {
        LinkedList linkedList = new LinkedList();
        if (VERSION.SDK_INT >= 21 && context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager != null) {
                try {
                    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                    linkedList = activeNetworkInfo != null ? getDnsServerIpsFromLinkProperties(connectivityManager, activeNetworkInfo) : linkedList;
                } catch (SecurityException e) {
                    Logger.i(TAG, "getActiveNetworkInfo failed, exception:" + e.getClass().getSimpleName());
                }
            }
        }
        return linkedList.isEmpty() ? new String[0] : (String[]) linkedList.toArray(new String[linkedList.size()]);
    }

    private static LinkedList<String> getDnsServerIpsFromLinkProperties(ConnectivityManager connectivityManager, NetworkInfo networkInfo) {
        LinkedList<String> linkedList = new LinkedList();
        Network[] allNetworks = connectivityManager.getAllNetworks();
        if (allNetworks == null || allNetworks.length == 0) {
            return linkedList;
        }
        for (Network network : allNetworks) {
            NetworkInfo networkInfo2 = connectivityManager.getNetworkInfo(network);
            if (networkInfo2 != null && networkInfo2.getType() == networkInfo.getType()) {
                LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
                if (linkProperties != null) {
                    for (InetAddress hostAddress : linkProperties.getDnsServers()) {
                        linkedList.add(hostAddress.getHostAddress());
                    }
                }
            }
        }
        return linkedList;
    }

    public static NetworkInfo getNetworkInfo(Context context) {
        if (context != null) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager != null) {
                try {
                    return connectivityManager.getActiveNetworkInfo();
                } catch (SecurityException e) {
                    return null;
                }
            }
        }
        return null;
    }

    public static int getNetworkType(Context context) {
        return context != null ? getNetworkType(getNetworkInfo(context)) : 0;
    }

    public static int getNetworkType(NetworkInfo networkInfo) {
        if (networkInfo == null || !networkInfo.isConnected()) {
            return -1;
        }
        int type = networkInfo.getType();
        if (1 == type || 13 == type) {
            return 1;
        }
        if (type != 0) {
            return 0;
        }
        int i;
        type = networkInfo.getSubtype();
        switch (type) {
            case 1:
            case 2:
            case 4:
            case 7:
            case 11:
                i = 2;
                break;
            case 3:
            case 5:
            case 6:
            case 8:
            case 9:
            case 10:
            case 12:
            case 14:
            case 15:
                i = 3;
                break;
            case 13:
                i = 4;
                break;
            default:
                i = 0;
                break;
        }
        if (i != 0 || VERSION.SDK_INT < 25) {
            return i;
        }
        switch (type) {
            case 16:
                return 2;
            case 17:
                return 3;
            default:
                return 0;
        }
    }
}
